package com.yuncheng.oaApi.spcy;

import com.yuncheng.entity.YcSysUser;
import com.yuncheng.oaApi.vo.*;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.oaApi.constant.OaJcyFybxdConstant;
import com.yuncheng.oaApi.constant.OaSpcyJcyFybxdConstant;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.oaApi.bxdService.OaSpcyJcyFybxdService;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.math.BigDecimal;
import java.util.*;

/**
 * @author hua
 */
@Api(tags = "审评查验检查员费用报销单管理")
@RestController
@RequestMapping("/api/spzx-oa/spcyjcyfybxd")
public class OaSpcyJcyFybxdController {

    @Resource
    private OaSpcyJcyFybxdService oaSpcyJcyFybxdService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdService spcyJcyFybxdServices;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxdgrjlService spcyJcyFybxdgrjlService;


    @Resource
    @Lazy
    private INkBxJtxcdService nkBxJtxcdService;

    @Resource
    @Lazy
    private INkRsZzsqdService nkRsZzsqdService;

    @Resource
    @Lazy
    private IOaSpcyJcyFybxxmmxjlService spcyJcyFybxxmmxjlService;


    @Resource
    @Lazy
    private INkRsXmyszbbService nkRsXmyszbbService;


    @Log(title = "查询费用报销单-分页列表")
    @ApiOperation(value = "查询费用报销单-分页列表")
    @GetMapping(value = "queryFybxdPageList")
    public HttpResult queryFybxdPageList(OaSpcyJcyFybxd fybxd, String gjz, PageVo pageVo, OrderItemVo orderItemVo){
        return oaSpcyJcyFybxdService.queryFybxdPageList(fybxd, gjz, false, pageVo, orderItemVo);
    }

    /*@Log(title = "查询我的费用报销单-分页列表")
    @ApiOperation(value = "查询我的费用报销单-分页列表")*/
    @GetMapping(value = "queryMyFybxdPageList")
    public HttpResult queryMyFybxdPageList(OaSpcyJcyFybxd fybxd, String gjz, PageVo pageVo, OrderItemVo orderItemVo){
        return oaSpcyJcyFybxdService.queryFybxdPageList(fybxd, gjz, true, pageVo, orderItemVo);
    }



    /*@Log(title = "查询报销单列表-根据sqsxid")
    @ApiOperation(value = "查询报销单列表-根据sqsxid")*/
    @GetMapping(value = "queryBxdListBySqsxId")
    public HttpResult queryBxdListBySqsxId(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

        List<OaSpcyJcyFybxd> list = oaSpcyJcyFybxdService.queryBxdListBySqsxId(sqsxid);

        return HttpResultVo.HttpResultList(list);
    }


    @Log(title = "查询报销人员信息列表-根据sqsxid")
    @ApiOperation(value = "查询报销人员信息列表-根据sqsxid")
    @GetMapping(value = "queryBxryxxListBySqsxId")
    public HttpResult queryBxryxxListBySqsxId(@RequestParam(value = "sqsxid",defaultValue = "")String sqsxid,@RequestParam(value = "bxdid",defaultValue = "")String bxdid){


        return HttpResult.ok(oaSpcyJcyFybxdService.queryBxryxxListBySqsxId(sqsxid,bxdid));
    }

    @Log(title = "查询报销人员记录列表-根据申请事项id")
    @ApiOperation(value = "查询报销人员记录列表-根据申请事项id")
    @GetMapping(value = "queryBxryjlBySqsxId")
    public HttpResult queryBxryjlBySqsxId(@RequestParam(value = "sqsxid",defaultValue = "SQSXID")String sqsxid){


        return HttpResultVo.HttpResultList(oaSpcyJcyFybxdService.queryBxryjlBySqsxId(sqsxid));
    }

    /*@Log(title = "删除费用报销单及明细")
    @ApiOperation(value = "删除费用报销单及明细")*/
    @GetMapping(value = "deleteFybxdAndRymx")
    public HttpResult deleteFybxdAndRymx(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        oaSpcyJcyFybxdService.deleteFybxdAndRymx(fbxdid);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "查询专家劳务费列表-根据报销单id")
    @ApiOperation(value = "查询专家劳务费列表-根据报销单id")
    @GetMapping(value = "queryZjlwfList")
    public HttpResult queryZjlwfList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdService.queryZjlwfList(fbxdid);

        return HttpResultVo.HttpResultList(list);
    }

    @Log(title = "查询报销人员信息列表-根据报销单id")
    @ApiOperation(value = "查询报销人员信息列表-根据报销单id")
    @GetMapping(value = "queryBxryxxListByBxdId")
    public HttpResult queryBxryxxListByBxdId(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        List<OaSpcyJcyFybxdgrjl> list = oaSpcyJcyFybxdService.queryBxryxxListByBxdId(fbxdid);

        return HttpResultVo.HttpResultList(list);
    }

    @Log(title = "查询报销单信息-根据报销单id")
    @ApiOperation(value = "查询报销单信息-根据报销单id")
    @GetMapping(value = "queryBxdById")
    public HttpResult queryBxdById(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){
        return HttpResult.ok(oaSpcyJcyFybxdService.queryBxdById(fbxdid));
    }

    @Log(title = "查询报销单及人员明细-根据报销单id")
    @ApiOperation(value = "查询报销单及人员明细-根据报销单id")
    @GetMapping(value = "queryBxdMxById")
    public HttpResult queryBxdMxById(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){
        Map resultMap = new HashMap();
        OaSpcyJcyFybxd fybxd = oaSpcyJcyFybxdService.queryBxdById(fbxdid);
        List<OaSpcyJcyFybxdgrjl> bxmxList = oaSpcyJcyFybxdService.queryBxryxxListByBxdId(fbxdid);

        resultMap.put("bxd", fybxd);
        resultMap.put("bxmxList", bxmxList);
        return HttpResult.ok(resultMap);
    }

    @Log(title = "查询可选择人员-分页列表")
    @ApiOperation(value = "查询可选择人员-分页列表")
    @GetMapping(value = "queryKSelectUserPageList")
    public HttpResult queryKSelectUserPageList(YcSysUser query, String gjz, PageVo pageVo){
        return oaSpcyJcyFybxdService.queryKSelectUserPageList(query, gjz, pageVo);
    }

    @Log(title = "新增报销人员用户信息")
    @ApiOperation(value = "新增报销人员用户信息")
    @PostMapping(value = "addBxryUserInfo")
    public HttpResult addBxryUserInfo(@RequestBody AddJcyBxryInfoVo jcyBxryInfoVo) throws InvocationTargetException, IllegalAccessException {

        return oaSpcyJcyFybxdService.addBxryUserInfo(jcyBxryInfoVo);
    }

    @Log(title = "保存选择报销单-报销人员")
    @ApiOperation(value = "保存选择报销单-报销人员")
    @GetMapping(value = "saveSelectBxry")
    public HttpResult saveSelectBxry(@RequestParam(value = "ids",defaultValue = "")String ids,
                                     @RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                     @RequestParam(value = "fjcsf",defaultValue = "")String fjcsf,
                                     @RequestParam(value = "sqsxid",defaultValue = "")String sqsxid){

       return oaSpcyJcyFybxdService.saveSelectBxry(ids, fbxdid, sqsxid, fjcsf);
    }

    @Log(title = "删除报销人员明细-根据报销人员id")
    @ApiOperation(value = "删除报销人员明细-根据报销人员id")
    @DeleteMapping(value = "deleteBxrymxById")
    public HttpResult deleteBxrymxById(@RequestParam(value = "id",defaultValue = "")String id){

        oaSpcyJcyFybxdService.deleteBxrymxById(id);

        return HttpResult.ok("删除成功");
    }

    @Log(title = "更新报销单信息")
    @ApiOperation(value = "更新报销单信息")
    @PostMapping(value = "updateBxd")
    public HttpResult updateBxd(@RequestBody HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {

        oaSpcyJcyFybxdService.updateBxd(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "更新报销人员明细信息")
    @ApiOperation(value = "更新报销人员明细信息")
    @PostMapping(value = "updateBxrymxxx")
    public HttpResult updateBxrymxxx(@RequestBody HashMap<String,String> buffer) throws IllegalAccessException, InvocationTargetException {

        oaSpcyJcyFybxdService.updateBxrymxxx(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "查询当前记录的对应垫付人员列表")
    @ApiOperation(value = "查询当前记录的对应垫付人员列表")
    @GetMapping(value = "queryFybxdDfryList")
    public HttpResult queryFybxdDfryList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                         @RequestParam(value = "id",defaultValue = "")String id){

        if (StringUtils.isBlank(fbxdid)){
            return HttpResult.error("参数为空！");
        }
        if (StringUtils.isBlank(id)){
            id = "135qr";
        }
        List<Map> mapList = oaSpcyJcyFybxdService.queryFybxdDfryList(fbxdid, id);

        return HttpResult.ok(mapList);
    }

    @Log(title = "查询申请人员列表")
    @ApiOperation(value = "查询申请人员列表")
    @GetMapping(value = "queryFybxdSqryList")
    public HttpResult queryFybxdSqryList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        if (StringUtils.isBlank(fbxdid)){
            return HttpResult.error("参数为空！");
        }
        List<Map> mapList = oaSpcyJcyFybxdService.queryFybxdDfryList(fbxdid, null);

        return HttpResultVo.HttpResultList(mapList);
    }

    @Log(title = "报销单信息-下一步")
    @ApiOperation(value = "报销单信息-下一步")
    @GetMapping(value = "submitFybxd")
    public HttpResult submitFybxd(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        return oaSpcyJcyFybxdService.submitFybxd(fbxdid);
    }

    @Log(title = "报销单信息-上一步")
    @ApiOperation(value = "报销单信息-上一步")
    @GetMapping(value = "returnFybxd")
    public HttpResult returnFybxd(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid){

        return oaSpcyJcyFybxdService.returnFybxd(fbxdid);
    }

    @Log(title = "批量设置出行方式")
    @ApiOperation(value = "批量设置出行方式")
    @GetMapping(value = "batchSetBxdCxfs")
    public HttpResult batchSetBxdCxfs(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                      @RequestParam(value = "fcsjjtfs",defaultValue = "")String fcsjjtfs){

        oaSpcyJcyFybxdService.batchSetBxdCxfsByBxdId(fbxdid, fcsjjtfs);

        return HttpResult.ok("设置成功");
    }

    /** 2024-02-26 新增*/
    @Log(title = "批量设置劳务费/伙食/交通补助天数")
    @ApiOperation(value = "批量设置劳务费/伙食/交通补助天数")
    @GetMapping(value = "batchSzLwHsJtbzTs")
    public HttpResult batchSzLwHsJtbzTs(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                        @RequestParam(value = "fjcts",defaultValue = "")String fjcts){
        oaSpcyJcyFybxdService.batchSzLwHsJtbzTs(fbxdid, fjcts);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "特殊事项申请-分页查询")
    @ApiOperation(value = "特殊事项申请-分页查询")
    @GetMapping(value = "queryFybxdTssxSqPageList")
    public HttpResult queryFybxdTssxSqPageList(@RequestParam(value = "fbxdid",defaultValue = "")String fbxdid,
                                               PageVo pageVo){

        return oaSpcyJcyFybxdService.queryFybxdTssxSqPageList(fbxdid, pageVo);
    }

    @Log(title = "特殊事项申请-根据id查看")
    @ApiOperation(value = "特殊事项申请-根据id查看")
    @GetMapping(value = "queryFybxdTssxSqById")
    public HttpResult queryFybxdTssxSqById(@RequestParam(value = "id",defaultValue = "")String id){

        return HttpResult.ok(oaSpcyJcyFybxdService.queryFybxdTssxSqById(id));
    }

    @Log(title = "特殊事项申请-新增")
    @ApiOperation(value = "特殊事项申请-新增")
    @PostMapping(value = "addFybxdTssxSq")
    public HttpResult addFybxdTssxSq(@RequestBody OaSpcyJcyFybxdtssxsq bean){

        return HttpResultVo.HttpResultSave(oaSpcyJcyFybxdService.addFybxdTssxSq(bean), "添加成功");
    }

    @Log(title = "特殊事项申请-更新")
    @ApiOperation(value = "特殊事项申请-更新")
    @PutMapping(value = "updateFybxdTssxSq")
    public HttpResult updateFybxdTssxSq(@RequestBody HashMap<String,String> buffer) throws InvocationTargetException, IllegalAccessException {
        oaSpcyJcyFybxdService.updateFybxdTssxSq(buffer);

        return HttpResult.ok("保存成功");
    }

    @Log(title = "特殊事项申请-删除")
    @ApiOperation(value = "特殊事项申请-删除")
    @GetMapping(value = "deleteFybxdTssxSq")
    public HttpResult deleteFybxdTssxSq(@RequestParam(value = "id",defaultValue = "")String id){
        oaSpcyJcyFybxdService.deleteFybxdTssxSq(id);

        return HttpResult.ok("删除成功");
    }



    @Log(title = "新增转账申请单")
    @ApiOperation(value = "新增转账申请单")
    @GetMapping(value = "addzzsq")
    public HttpResult addzzsq(NkRsZzsqd nkRsZzsqd)  {
        Result addzzsq = oaSpcyJcyFybxdService.addzzsq(nkRsZzsqd);

        if (addzzsq.isSuccess()){
            LambdaUpdateWrapper<OaSpcyJcyFybxdgrjl> updategrjl  = new LambdaUpdateWrapper<>();
            updategrjl.eq(OaSpcyJcyFybxdgrjl::getId, nkRsZzsqd.getFbxdgrjlid());
            updategrjl.eq(OaSpcyJcyFybxdgrjl::getFbxdid, nkRsZzsqd.getFbxdid());

            OaSpcyJcyFybxdgrjl oaSpcyJcyFybxdgrjl = new OaSpcyJcyFybxdgrjl();
            oaSpcyJcyFybxdgrjl.setFsfzz(OaJcyFybxdConstant.FKFS_ZZ_YES);
            if (nkRsZzsqd.getFwfqk().equals(OaJcyFybxdConstant.ZZWFQK_QQC)){
                oaSpcyJcyFybxdgrjl.setFsfqc(OaJcyFybxdConstant.FKFS_ZZ_YES);
            }
            spcyJcyFybxdgrjlService.update(oaSpcyJcyFybxdgrjl, updategrjl);




        }
        return HttpResultVo.HttpResultList(addzzsq.getResult());
    }


    @Log(title = "删除转账申请单")
    @ApiOperation(value = "删除转账申请单")
    @GetMapping(value = "dezzsq")
    public HttpResult dezzsq(String id)  {
        //删除转账申请单的时候 清空 垫付人名称 和姓名
        NkRsZzsqd nkRsZzsqd = nkRsZzsqdService.getById(id);
        List<NkBxJtxcd> nkBxJtxcds = nkBxJtxcdService.list(new LambdaQueryWrapper<NkBxJtxcd>()
                .eq(NkBxJtxcd::getId, nkRsZzsqd.getFjtxcdid()));
        if (CollectionUtils.isNotEmpty(nkBxJtxcds)){
            NkBxJtxcd nkBxJtxcd = nkBxJtxcds.get(0);
            if (nkRsZzsqd.getFwfqk().equals(OaJcyFybxdConstant.ZZWFQK_QC)){
                nkBxJtxcd.setFwjtfdfrxm("");
            }
            if (nkRsZzsqd.getFwfqk().equals(OaJcyFybxdConstant.ZZWFQK_FC)){
                nkBxJtxcd.setFfjtfdfrxm("");
            }
            nkBxJtxcdService.updateById(nkBxJtxcd);
        }

        nkRsZzsqdService.removeById(id);
        return HttpResult.ok("删除成功");
    }



    @Log(title = "编辑")
    @ApiOperation(value = "编辑")
    @PutMapping(value = "edzzsq")
    public HttpResult edzzsq(NkRsZzsqd nkRsZzsqd)  {
        nkRsZzsqdService.updateById(nkRsZzsqd);
        return HttpResult.ok("修改成功");
    }

    /**
     * 获取转账申请单。如果当前报销单有多个申请单 将收款单位相同的的金额相加并返回
     */

    @Log(title = "获取转账申请单列表")
    @ApiOperation(value = "获取转账申请单列表")
    @GetMapping(value = "getzzsqd")
    public HttpResult getzzsqd(String fbxdid)  {
        //收款单位是定死的 只有两家
        List<NkRsZzsqd> nkRsZzsqds = nkRsZzsqdService.list(new LambdaQueryWrapper<NkRsZzsqd>()
                .eq(NkRsZzsqd::getFbxdid, fbxdid));

        if (CollectionUtils.isNotEmpty(nkRsZzsqds)){
            BigDecimal nyhkbigDecimal = new BigDecimal(0);
            BigDecimal ffhkbigDecimal = new BigDecimal(0);

            Map nyhkmap = new HashMap();
            Map ffhkmap = new HashMap();

            ArrayList list = new ArrayList();
            for (NkRsZzsqd nkRsZzsqd:nkRsZzsqds){
                String fskdw = nkRsZzsqd.getFskdw();
                if (fskdw.equals(OaSpcyJcyFybxdConstant.FJ_SKDW_NYHKFWYXGW)){
                    nyhkbigDecimal = nyhkbigDecimal.add(new BigDecimal(nkRsZzsqd.getFjexx()));
//                    Map nyhkmap = new HashMap();
                    nyhkmap.put("mc", OaSpcyJcyFybxdConstant.FJ_SKDW_NYHKFWYXGW);
                    nyhkmap.put("khyh", nkRsZzsqd.getFkhyh());
                    nyhkmap.put("bxzh", nkRsZzsqd.getFzh());
                    nyhkmap.put("je", nyhkbigDecimal);

                }

                if (fskdw.equals(OaSpcyJcyFybxdConstant.FJ_SKDW_FFSWFWYXGS)){
                    ffhkbigDecimal = ffhkbigDecimal.add(new BigDecimal(nkRsZzsqd.getFjexx()));
//                    Map ffhkmap = new HashMap();
                    ffhkmap.put("mc", OaSpcyJcyFybxdConstant.FJ_SKDW_FFSWFWYXGS);
                    ffhkmap.put("khyh", nkRsZzsqd.getFkhyh());
                    ffhkmap.put("bxzh", nkRsZzsqd.getFzh());
                    ffhkmap.put("je", ffhkbigDecimal);

                }
            }
            if(!nyhkmap.isEmpty()){
                list.add(nyhkmap);
            }

            if (!ffhkmap.isEmpty()){
                list.add(ffhkmap);
            }

            return HttpResult.ok(list);
        }
        return HttpResult.ok();
    }


    /**
     * 查询报销明细
     */
    @Log(title = "查询报销明细")
    @ApiOperation(value = "查询报销明细")
    @GetMapping(value = "getbxmx")
    public HttpResult getbxmx(String fbxdid)  {
        List<OaSpcyJcyFybxxmmxjl> oaSpcyJcyFybxxmmxjls = spcyJcyFybxxmmxjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxxmmxjl>()
                .eq(OaSpcyJcyFybxxmmxjl::getFbxdid, fbxdid));

        if (CollectionUtils.isEmpty(oaSpcyJcyFybxxmmxjls)){
            return HttpResult.ok(null);
        }else {
            return HttpResult.ok(oaSpcyJcyFybxxmmxjls);
        }
    }

    @Log(title = "项目表格")
    @ApiOperation(value = "项目表格")
    @GetMapping(value = "xmbg")
    public HttpResult xmbg(String fbxdid)  {
        OaSpcyJcyFybxd oaSpcyJcyFybxd = spcyJcyFybxdServices.getById(fbxdid);

        Map map = new HashMap();

        //获取预算项目名称
        List<String> ysxmlist = Arrays.asList(oaSpcyJcyFybxd.getFysxmid().split(",|,"));

        for (String ysxmidlist:ysxmlist){

            NkRsXmyszbb rsXmyszbb = nkRsXmyszbbService.getById(ysxmidlist);
//            map.put("ysxmmc", rsXmyszbb.getFysxmmc());
            if (rsXmyszbb.getFbmjjflmc().equals(OaJcyFybxdConstant.BXXMFL_CLF)){

                map.put("clmc", rsXmyszbb.getFysxmmc());
            }else {

                map.put("lwmc", rsXmyszbb.getFysxmmc());
                map.put("gsmc",rsXmyszbb.getFysxmmc() + "(个税)");
            }

        }
        //个税
        List<OaSpcyJcyFybxdgrjl> jcyFybxdgrjls = spcyJcyFybxdgrjlService.list(new LambdaQueryWrapper<OaSpcyJcyFybxdgrjl>()
                .eq(OaSpcyJcyFybxdgrjl::getFbxdid, fbxdid));

        BigDecimal gsje = new BigDecimal(0);

        for (OaSpcyJcyFybxdgrjl jcyFybxdgrjl:jcyFybxdgrjls){
            String flwfgsje = Optional.ofNullable(jcyFybxdgrjl.getFlwfgsje()).orElseGet(()->"0") ;
            gsje = gsje.add(new BigDecimal(flwfgsje));
        }

        //劳务费金额 与 差旅费金额
        String fclfbxje = oaSpcyJcyFybxd.getFclfbxje();
        String flwfbxje = oaSpcyJcyFybxd.getFlwfbxje();


        map.put("gsje",String.valueOf(gsje));
        map.put("lwje",flwfbxje);
        map.put("clje",fclfbxje);

        return HttpResult.ok(map);
    }


    /**
     * 检查员查询当前申请的报销单
     */


    @Log(title = "同步更新报销单人员的银行卡相关信息")
    @ApiOperation(value = "同步更新报销单人员的银行卡相关信息")
    @GetMapping(value = "tybxjlxx")
    public HttpResult tybxjlxx(@RequestParam(value = "id",defaultValue = "") String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("参数为空！");
        }

        return oaSpcyJcyFybxdService.syncGrjlUserJbxx(null, id);
    }






}
